/*	$Id: dlink_file.c 7 2008-07-29 02:58:11Z phrakt $	*/
/*
 * Copyright (c) 2003 Jean-Francois Brousseau <jfb@openbsd.org>
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
 * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 *
 * This file contains code to create an abstraction layer between the contents
 * of a file and the Packet Forge datalink interface.  Using this layer,
 * Packet Forge can read from and write to files as if they were streams of
 * network traffic.
 *
 * The file format is very straight-forward.  It starts with a header structure
 * containing a magic number (to detect if a file is of the right format) and
 * a major and minor version in case the format changes.  After the header,
 * packets are written in binary with a packet header prepended, which simply
 * gives the length in bytes of the packet following it.
 */


#include <sys/types.h>
#include <sys/uio.h>
#include <sys/time.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

#include <net/if.h>

#include <err.h>
#include <poll.h>
#include <stdio.h>
#include <fcntl.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "if.h"
#include "dlink.h"
#include "pforge.h"


#define PFDL_FILE_MAGIC    0xcafed00d
#define PFDL_FILE_MAJOR    1
#define PFDL_FILE_MINOR    2



struct pfdl_filehdr {
	uint32_t  magic;
	uint16_t  major;
	uint16_t  minor;
};


struct pfdl_pkthdr {
	uint32_t  snaplen;
};





int        dlink_file_open  (PF_DLINK *, const char *);
int        dlink_file_close (PF_DLINK *);
int        dlink_file_bind  (PF_DLINK *, struct pfnet_if *);
int        dlink_file_ctl   (PF_DLINK *, int, void *);
ssize_t    dlink_file_read  (PF_DLINK *, void *, size_t);
ssize_t    dlink_file_write (PF_DLINK *, void *, size_t);


struct dlink_ops dlink_file_ops = {
	dlink_file_open,
	dlink_file_close,
	dlink_file_bind,
	dlink_file_ctl,
	dlink_file_read,
	dlink_file_write
};


/*
 * dlink_file_open()
 *
 * Open a file containing packets dumped by Packet Forge or another program
 * using the same format.
 * Returns -1 on failure.
 */

int
dlink_file_open(PF_DLINK *dl)
{
	int file_fd;
	struct pfdl_filehdr hdr;

	if ((file_fd = open(dev, O_RDWR)) == -1) {
		return (-1);
	}

	if (read(file_fd, &hdr, sizeof(hdr)) == -1) {
		(void)close(file_fd);
		return (-1);
	}

	if (hdr.magic != PFDL_FILE_MAGIC) {
		(void)close(file_fd);
		return (-1);
	}

	strlcpy(dl->dldev, dev, sizeof(dl->dldev));

	dl->dlfd = file_fd;

	return (file_fd);
}


/*
 * dlink_file_close()
 *
 *
 */

int
dlink_file_close(PF_DLINK *dl)
{
	if (dl == NULL)
		return (-1);

	return 0;
}


/*
 * dlink_file_bind()
 *
 * Bind the dlink layer structure <dl> to the network interface <bif>.
 * In order to make sure that reads from the BPF device will not return
 * truncated packets, we set the capture length to <bif>'s MTU.
 *
 */

int
dlink_file_bind(PF_DLINK *dl, struct pfnet_if *bif)
{
	return (0);
}


/*
 * dlink_file_ctl()
 *
 */

int
dlink_file_ctl(PF_DLINK *dl, int type, void *data)
{
	return (0);
}


/*
 * dlink_file_read()
 *
 * Read bytes from the BPF device.
 */

ssize_t
dlink_file_read(PF_DLINK *dl, void *dst, size_t len)
{
	ssize_t bytes;
	uint8_t bigbuf[2048];

	bytes = read(dl->dlfd, dl->rbuf, dl->rbufsz);
	if (bytes < 0) {
		return (-1);
	} else if (bytes == 0) {
		return (-1);
	}

	return (0);
}


/*
 * dlink_file_write()
 *
 * Write a packet to the datalink layer.
 *
 *
 */

ssize_t
dlink_file_write(PF_DLINK *dl, void *src, size_t len)
{
	ssize_t sbytes;

	sbytes = 0;

	return sbytes;
}
